<%@ page import="com.scalar.chiptrack.utils.ConfigManager,
                 com.scalar.forecast.forecastEntry.SearchField,
                 java.util.ArrayList,
                 com.scalar.forecast.forecastEntry.forms.ForecastEntrySearchForm,
                 com.scalar.forecast.commons.Constants,
                 com.scalar.forecast.forecastSetup.ForecastEntryField"%>
<%@ taglib uri="/WEB-INF/struts-html.tld"  prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld"  prefix="logic"%>
<%@ taglib uri="/WEB-INF/struts-nested.tld"  prefix="nested"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld"  prefix="bean"%>
<%@ taglib uri="/WEB-INF/chiptrack-tags.tld" prefix="chiptrack" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>Scalarsoft - ChipTrac</TITLE>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<% try{ %>
<%String chipTracContext = "/" + ConfigManager.getChipTracContext();%>

<LINK href="<%=chipTracContext%>/jsp/includes/stylesheets/scalarsoft.css" type=text/css rel=stylesheet>
<%
    String isBuild = ( String )request.getAttribute ( Constants.FORECAST_IS_BUILD );
%>
<SCRIPT language=JavaScript src="<%=chipTracContext%>/jsp/includes/scripts/validations.js">
</SCRIPT>
<SCRIPT language=JavaScript src="<%=chipTracContext%>/jsp/includes/scripts/functions.js">
</SCRIPT>
<SCRIPT>
            var stack = new Array();
            var stackPtr = 0;
            function push ( obj )
            {
                stack [ stackPtr++ ] = obj;
            }
            function pop()
            {
                if ( stackPtr == 0 )
                {
                    return null;
                }
                return stack[ --stackPtr ];
            }
            function toggle ( level, img )
            {
                pushChilds ( level );
                childNode = pop();
                if ( childNode == null )
                {
                    return;
                }
                var displayStatus = '';
                if ( childNode.style.display == 'block' )
                {
                    displayStatus = 'none';
                    childNode.style.display = displayStatus;
                    if ( img != null )
                        img.src ="<%=chipTracContext%>/jsp/includes/images/plus.gif";
                    pushChilds ( childNode );
                }
                else
                {
                    displayStatus = 'block'
                    if ( img != null )
                        img.src ="<%=chipTracContext%>/jsp/includes/images/minus.gif";
                    childNode.style.display = displayStatus;
                }
                imgElmt = document.getElementById( childNode.id + 'img' );
                if ( imgElmt != null )
                {
                    if ( displayStatus == 'block' )
                        imgElmt.src = "<%=chipTracContext%>/jsp/includes/images/plus.gif";
                }
                while ( true )
                {
                    childNode = pop();
                    if ( childNode == null )
                    {
                        return;
                    }
                    childNode.style.display = displayStatus;
                    imgElmt = document.getElementById( childNode.id + 'img' );
                    if ( imgElmt != null )
                    {
                        if ( displayStatus == 'block' )
                            imgElmt.src = "<%=chipTracContext%>/jsp/includes/images/plus.gif";
                    }
                    if ( displayStatus == 'none' ) //sub childNodes also needs to be hidden.
                        pushChilds ( childNode )
               }
            }
            function pushChilds ( level )
            {
                var i = 1;
                while ( true )
                {
                    childNode1 = document.getElementById ( level.id + "c" + i );
                    if ( childNode1 != null )
                    {
                        push ( childNode1 );
                    }
                    else
                    {
                        break;
                    }
                    i++;
                }
           }
		   function toggleEnableDisable( chkbox, lineNo, noOfMths ) //lineNo --> starts from zero
		   {
				var form = document.salesForecast;
				var startIdx = parseInt ( lineNo, 10 ) * parseInt ( noOfMths, 10 )

				if ( chkbox.checked == true )
				{
					for ( i=0; i < noOfMths; i++,startIdx++ )
					{
						form.qty[startIdx].disabled = false;
						form.price[startIdx].disabled = false;
					}
				}
				else
				{
					for ( i=0; i < noOfMths; i++,startIdx++ )
					{
						form.qty[startIdx].disabled = true;
						form.price[startIdx].disabled = true;
					}
				}
		   }
		   function showSum ( lineNo, monthNo, noOfMths ) //lineNo, monthNo --> starts from zero
		   {
				var form = document.salesForecast;
				var resultIdx = ( ( parseInt ( lineNo, 10 ) * parseInt ( noOfMths, 10 ) ) + monthNo ) % noOfMths;
				var nextIdx = resultIdx + noOfMths;
				sum = 0;
				for ( ;; nextIdx = nextIdx + noOfMths )
				{
					var ctrl = form.qty[nextIdx];

					if ( ctrl == null )
					{
						form.qty[resultIdx].value = sum;

						return;
					}
					else
					{
						sum = sum + parseInt ( ctrl.value, 10 );
					}
				}

		   }
		   function showAvg ( lineNo, monthNo, noOfMths ) //lineNo, monthNo --> starts from zero
		   {
				var form = document.salesForecast;
				var resultIdx = ( ( parseInt ( lineNo, 10 ) * parseInt ( noOfMths, 10 ) ) + monthNo ) % noOfMths;
				var nextIdx = resultIdx + noOfMths;
				sum = 0;
				count = 0;
				for ( ;; nextIdx = nextIdx + noOfMths )
				{
					var ctrl = form.price[nextIdx];

					if ( ctrl == null )
					{
						var result = Math.round((sum/count)*100)/100; //cutting down to upto 2 decimal places.
						result = result + "";
						var arr = result.split( '.' );
						if ( arr.length == 1 )
						{
							result = result + ".00";
						}
						else if ( arr[1].length == 0 )
						{
							result = result + "00";

						}
						else if ( arr[1].length == 1 )
						{
							result = result + "0"
						}
						form.price[resultIdx].value = result;
						return;
					}
					else
					{
						sum = sum + parseFloat ( ctrl.value );
						count++;
					}
				}
		   }
		   function openEditColumnWindow()
		   {
				newWindow = window.open ('forecast_entry_edit_column_display.html','editColumnPopup','channelmode=no,directories=no,toolbar=no,menubar=no,personalbar=no,status=yes,scrollbars=auto,width=525 height=400,resizable=yes,left=300,top=130');
				 newWindow.focus();
		   }
		   function openExportFileWindow()
		   {
				newWindow = window.open ('export_to_file.html','exportToFilePopup','channelmode=no,directories=no,toolbar=no,menubar=no,personalbar=no,status=yes,scrollbars=no,width=300 height=270,resizable=no,left=550,top=180');
				 newWindow.focus();
		   }
           function loadDetails( ctrl )
           {
                var form = document.forecastEntrySearchForm;
                if ( typeof ctrl == 'undefined' )
                {
                    return;
                }
                var orderByValue;
                if ( form.orderBy.length != null )
                {
                    for ( i=0; i < form.orderBy.length; i++ )
                    {
                        if ( form.orderBy[i].checked == true )
                            orderByValue = form.orderBy[i].value.trim();
                    }
                    ctrlName = ctrl.name;
                    if ( orderByValue == '<%=Constants.FORECAST_SETUP_BY_CUSTOMER%>' )
                    {
                        if ( ctrlName == 'custId' )
                        {
                            if ( form.projectId != null )
                            {
                                form.projectId.value = '-1';
                                form.action = "forecastEntrySearchAction.do?action=loadValues&isBuild=<%=isBuild%>";
                                form.submit();
                            }
                            else if ( form.odmId != null )
                            {
                                form.action = "forecastEntrySearchAction.do?action=loadValues&isBuild=<%=isBuild%>";
                                form.submit();
                            }
                        }
                        else if ( ctrlName == 'projectId' )
                        {
                            form.action = "forecastEntrySearchAction.do?action=loadValues&isBuild=<%=isBuild%>";
                            form.submit();
                        }
                    }
                    else if ( orderByValue == '<%=Constants.FORECAST_SETUP_BY_PRODUCT%>' )
                    {
                        if ( ctrlName == 'productId' )
                        {
                            if ( form.projectId != null )
                            {
                                form.projectId.value = '-1';
                                form.custId.value = '-1';
                                form.action = "forecastEntrySearchAction.do?action=loadValues&isBuild=<%=isBuild%>";
                                form.submit();
                            }
                        }
                        if ( ctrlName == 'projectId' )
                        {
                            form.custId.value = '-1';
                            form.action = "forecastEntrySearchAction.do?action=loadValues&isBuild=<%=isBuild%>";
                            form.submit();
                        }
                        if ( ctrlName == 'custId' )
                        {
                            if ( form.odmId != null )
                            {
                                form.action = "forecastEntrySearchAction.do?action=loadValues&isBuild=<%=isBuild%>";
                                form.submit();
                            }
                        }
                    }
                    else if ( orderByValue == '<%=Constants.FORECAST_SETUP_BY_PROJECT%>' )
                    {
                        if ( ctrlName == 'projectId' )
                        {
                            form.productId.value = '-1';
                            form.custId.value = '-1';
                            form.action = "forecastEntrySearchAction.do?action=loadValues&isBuild=<%=isBuild%>";
                            form.submit();
                        }
                        else if ( ctrlName == 'custId' )
                        {
                            if ( form.odmId != null )
                            {
                                form.action = "forecastEntrySearchAction.do?action=loadValues&isBuild=<%=isBuild%>";
                                form.submit();
                            }
                        }
                    }
                }
           }
           errors = "";
           function onSubmit()
           {
                var form =  document.forecastEntrySearchForm;
                errors = "";
                var pa  = document.getElementById ( 'errorsId' );
                pa.innerHTML = "";
                var custError = "",projectError = "", productError = "";
                if ( form.custId != null  && form.custId.value == '-1'  )
                {
                    custError =  "<li>Please select Customer</li>"
                }
                if ( form.projectId != null  && form.projectId.value == '-1' )
                {
                    projectError = "<li>Please select Project</li>"
                }
                if ( form.productId != null  && ( form.productId.value == '-1' || form.productId.value == '0' ) )
                {
                    productError = "<li>Please select Product</li>"
                }
                var orderByValue = "";
                if ( form.orderBy.length != null )
                {
                    for ( i=0; i < form.orderBy.length; i++ )
                    {
                        if ( form.orderBy[i].checked == true )
                        {
                            orderByValue = form.orderBy[i].value.trim();
                            break;
                        }
                    }
                }
                else
                {
                    orderByValue = form.orderBy.value.trim();
                }
                if ( orderByValue == '' )
                {
                    errors = "<li>Please select Order By</li>"
                }
                if ( orderByValue == '<%=Constants.FORECAST_SETUP_BY_CUSTOMER%>' )
                {
                    errors = custError + projectError + productError;
                }
                else if ( orderByValue == '<%=Constants.FORECAST_SETUP_BY_PRODUCT%>' )
                {
                    errors = productError + projectError + custError;
                }
                else if ( orderByValue == '<%=Constants.FORECAST_SETUP_BY_PROJECT%>' )
                {
                    errors = projectError + productError + custError;
                }

                if ( trim ( form.showNext.value ) == '' )
                {
                    errors = errors + "<li>Please enter value for Show Next</li>"
                }
                if( !isInteger( form.showNext.value ) )
                {
                    errors = errors +"<li>  Please enter a valid Number for Show Next";
                }
                else if ( parseInt ( form.showNext.value, 10 ) <= 0 )
                {
                    errors = errors +"<li>  Please enter a valid Number for Show Next";
                }
                if ( form.startMonth_Quarter_Year.value == '-1' && form.year != null ) //time frame is by month or by quarter
                {
                    errors = errors + "<li>Please select Start Month/Quarter</li>"
                }
                else if ( form.startMonth_Quarter_Year.value == '-1' && form.year == null  ) //time frame is by Year.
                {
                    errors = errors + "<li>Please select Year</li>"
                }
                if ( form.year != null && form.year.value == -1 )
                {
                    errors = errors + "<li>Please select Year</li>"
                }
                if ( errors != ''  )
                {
                    pa.innerHTML = errors;
                    return;
                }
                form.action = <%= ( ( "true".equalsIgnoreCase( isBuild ) ? "'forecastEntryBuildAction.do?action=new'" : "'forecastEntrySalesAction.do?action=new'" ) ) %>;
                form.submit();
           }
           function loadValues()
           {
                var form =  document.forecastEntrySearchForm;
                if ( form.custId != null )
                    form.custId.value = '-1';
                if ( form.odmId != null )
                    form.odmId.value = '-1';
                if ( form.productId != null )
                    form.productId.value = '-1';
                if ( form.projectId != null )
                    form.projectId.value = '-1';

                form.action = "forecastEntrySearchAction.do?action=loadValues&isBuild=<%=isBuild%>";
                form.submit();
           }

        </SCRIPT>

<META content="MSHTML 6.00.2800.1458" name=GENERATOR></HEAD>
<BODY bgColor=#ffffff leftMargin=0 topMargin=0 marginwidth="0" marginheight="0">
<!-- Top Header Start-->
<%@ include file="/jsp/includes/header.jsp" %>
<!-- Top Header End-->
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0><!--TBODY-->
  <TBODY>
  <TR>
    <TD vAlign=top width=180 background="<%=chipTracContext%>/jsp/includes/images/left_red_bg.gif">
  <!-- Side Navigation start -->
    <%@ include file="/jsp/marketing/marketing_menu.jsp" %>
 <!-- end of the side navigations -->
    <TD bgColor=#ffffff><IMG height=9
      src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=8></TD>
    <TD bgColor=#ffffff><IMG height=480
      src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=10></TD>
    <TD vAlign=top width="100%"><IMG height=1
      src="<%=chipTracContext%>/jsp/includes/images/clear.gif" width=500 border=0> <BR>
<!-- from here your body starts -->
<html:form action="/forecastEntrySearchAction" name="forecastEntrySearchForm" type="com.scalar.forecast.forecastEntry.forms.ForecastEntrySearchForm">
      <TABLE width="100%" align=center border=0>
        <TBODY>
        <TR>
        <%
            String heading = ( "true".equalsIgnoreCase( isBuild )? "Build" : "Sales" );
        %>
          <TD class=NArialW align=middle colSpan=5><%=heading%> Forecast</TD></TR>
        <TR>
          <TD colSpan=5>&nbsp;</TD></TR>
        <TR><TD colspan='5' id='errorMessagesId'><chiptrack:errors styleClass='NArialL' /></TD></TR>
        <TR><TD colspan='5' id='infoMessagesId'><chiptrack:info styleClass='NArialL' /></TD></TR>
        <TR><TD colspan='5' class="NArialL"><FONT id="errorsId"  color='FF0000'></FONT></TD></TR>

        <TR>
            <TD colspan=5 class="NArialL">
                <logic:iterate name="forecastSetupFields" id="setupField" scope="request">
                    <logic:equal name="setupField" property="entrySelected" value="true">
                        <logic:notEqual name="setupField" property="entryName" value="By ODM">
                            <nested:radio property="orderBy" value="<%=( ( ForecastEntryField )setupField ).getEntryName()%>" onclick="loadValues();"/><bean:write name="setupField" property="entryName"/>&nbsp;&nbsp;&nbsp;
                        </logic:notEqual>
                    </logic:equal>
                </logic:iterate>
            </TD>
        </TR>
        <TR>
          <TD colSpan=5>&nbsp;</TD></TR>
        <TR>
          <TD>
            <TABLE width="100%" border=0>
              <TBODY>
              <% int tdIdxCnt = 0; %>
              <nested:iterate property="searchFieldList" indexId="idx" id="searchField">
                <nested:equal property="display" value="true">
                   <% Integer tdidx = new Integer ( (++tdIdxCnt)%2 );
                      pageContext.setAttribute( "tdidx",tdidx );%>
                   <logic:equal name="tdidx" value="1" scope="page">
                      <TR>
                        <TD width="20%"><FONT class=NArialL><nested:write property="displayName"/>:</FONT></TD>
                        <TD class=NArialL width="25%">
                            <nested:equal property="type" value="select">
                                <html:select name="forecastEntrySearchForm" property='<%=(( SearchField )searchField).getPropertyName()%>' styleClass="txfield" onchange="loadDetails(this)">
                                    <% ArrayList propValues = (( SearchField )searchField).getPropertyValues();
                                        pageContext.setAttribute( "propertyValues", propValues);%>
                                    <html:options collection="propertyValues" property="value" labelProperty="label" />
                                </html:select>
                            </nested:equal>
                            <nested:equal property="type" value="text">
                                <html:text name="forecastEntrySearchForm" property='<%=(( SearchField )searchField).getPropertyName()%>' styleClass="txfield" size='<%=(( SearchField )searchField).getDisplaySize()+""%>' onkeypress="javascript:allowInteger()" maxlength="2"/>
                                &nbsp;<bean:write name="showNext" scope="request"/>
                            </nested:equal>

                       </TD>
                   </logic:equal>
                   <logic:equal name="tdidx" value="0" scope="page">
                        <TD width="5%">&nbsp;</TD>
                        <TD class=NArialL width="20%"><nested:write property="displayName"/>:</TD>
                        <TD class=NArialL width="30%">
                            <nested:equal property="type" value="select">
                                <html:select name="forecastEntrySearchForm" property='<%=(( SearchField )searchField).getPropertyName()%>' styleClass="txfield" onchange="loadDetails(this)">
                                    <% ArrayList propValues = (( SearchField )searchField).getPropertyValues();
                                        pageContext.setAttribute( "propertyValues", propValues);%>
                                    <html:options collection="propertyValues" property="value" labelProperty="label" />
                                </html:select>
                            </nested:equal>
                            <nested:equal property="type" value="text">
                                <html:text name="forecastEntrySearchForm" property='<%=(( SearchField )searchField).getPropertyName()%>' styleClass="txfield" size='<%=(( SearchField )searchField).getDisplaySize()+""%>' onkeypress="javascript:allowInteger()" maxlength="2" />
                                &nbsp;<bean:write name="showNext" scope="request"/>
                            </nested:equal>
                       </TD>
                     </TR>
                   </logic:equal>
                </nested:equal>
              </nested:iterate>

              <logic:notEqual name="isBuild" value="true" scope="request">
                <TR>
                    <TD colspan="5"><nested:checkbox property="showActualValues"/> &nbsp;<FONT class="NArialL">Show Actual Values</FONT></TD>
                </TR>
                <TR>
                    <TD colspan="5"><nested:checkbox property="showAllSalesPersonsActualValues"/> &nbsp;<FONT class="NArialL">Show All SalesPersons Actual Values</FONT></TD>
                </TR>
              </logic:notEqual>
              <TR>
                <TD bgColor=#ffffff><IMG height=9
                  src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=8></TD></TR>
              <TR>
                <TD width="100%" colSpan=5 align="center">
                    <A href="javascript:onSubmit();"><IMG
					onmouseover="window.status='Click to Submit';return true"
					title="Click to Submit"
					onmouseout="window.status='';return true" height=17
					src="<%=chipTracContext%>/jsp/includes/images/submit.gif" width=53
					border=0></A>&nbsp;&nbsp;&nbsp;&nbsp;
                  <A href="javascript:document.forecastEntrySearchForm.reset();"><IMG
					onmouseover="window.status='Click to Reset';return true"
					title="Click here to Reset"
					onmouseout="window.status='';return true"
					src="<%=chipTracContext%>/jsp/includes/images/reset.gif"
					border=0 width="53" height="17"></A>
                </TD>
                </TR>
            </TBODY></TABLE></TD></TR>
</html:form>

        <TR>
          <TD><IMG src="<%=chipTracContext%>/jsp/includes/images/spacer.gif"
width=10></TD></TR>


		 </TBODY></TABLE></TD>

<!-- Footer starts here -->
<%@ include file="/jsp/includes/footer.jsp" %>
<!-- footer end here -->
</BODY>
</HTML>
<%}catch ( Exception e){ out.println ( e); }%>